﻿
namespace Leetcode.N0937;
public class Solution
{
    public string[] ReorderLogFiles(string[] logs)
    {
        return logs.OrderBy(i => i, new Comparer()).ToArray();
    }

    public class Comparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            var posx = x.IndexOf(' ');
            var posy = y.IndexOf(' ');

            if (char.IsDigit(x[posx + 1]) && char.IsDigit(y[posy + 1]))
            {
                return 0;
            }

            if (char.IsDigit(x[posx + 1]) && !char.IsDigit(y[posy + 1]))
            {
                return 1;
            }

            if (!char.IsDigit(x[posx + 1]) && char.IsDigit(y[posy + 1]))
            {
                return -1;
            }

            var compareResult = string.Compare(x, posx + 1, y, posy + 1, Math.Max(x.Length - posx - 1, y.Length - posy - 1));
            if (compareResult != 0)
            {
                return compareResult;
            }
            return string.Compare(x, 0, y, 0, Math.Max(posx, posy));
        }
    }
}